package cz.muni.fi.pa165.dundd.daoimpl;

import cz.muni.fi.pa165.dundd.entity.Troop;
import cz.muni.fi.pa165.dundd.dao.TroopDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

/**
 *
 * @author xceluch
 */
@Repository
public class TroopDAOImpl implements TroopDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void createTroop(Troop troop) {
        em.persist(troop);
    }

    @Override
    public void updateTroop(Troop troop) {
        em.merge(troop);
    }

    @Override
    public void deleteTroop(Troop troop) {
        em.remove(em.contains(troop) ? troop : em.merge(troop));
    }

    @Override
    public List<Troop> retrieveAllTroops() {
        return em.createQuery("SELECT t FROM Troop t", Troop.class).getResultList();
    }

    @Override
    public Troop retrieveTroopById(Long id) {
        return em.find(Troop.class, id);
    }
}
